<?php defined('SYSPATH') or die('No direct script access.');

// URL: /<context>/index.php/Home/
class Controller_Mbox extends Controller_Base 
{
	const MODULE_NAME = "Message";
	
	private $messageService;
	private $projectMemberService;
	
	public function __construct(Request $request, Response $response)
	{
		parent::__construct($request, $response);
		$this->messageService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_MESSAGE.'/', ACCESS_TOKEN);
		$this->messageService->init($this->_requestContext);
		
		$this->projectMemberService = new Service_GenericSeedniaServiceStub(WSROOT.WSCONTEXT.'/'.SERVICE_PROJECT.'/member/', ACCESS_TOKEN);
		$this->projectMemberService->init($this->_requestContext);
	}
	
	public function action_index()
	{
		$contentFrame = $this->index('Seednia::Project', 'default_sbar_right', '');
		$content = $this->createView('mbox_content');
		$contentFrame->content = $content;

		$this->_projectHelper->loadUserProjects($this->template);

		$messageCriteria = array('recipientSid' => $this->_sessionInfo->userInfo['userSid']);
		$content->messages = $this->messageService->getList($messageCriteria, 0, 20);
		
		// List invitations
		$memberRequestCriteria = array('userSid' => $this->_sessionInfo->userInfo['userSid']);
		$memberRequestCriteria['status'] = 1;
		$content->invitations = $this->projectMemberService->getList($memberRequestCriteria, 0, 20, 'request/');
	}
	
	public function action_form()
	{
		if ( $this->isMethodGet() ) {
			$this->showForm();
		} else if ( $this->isMethodPost() ) {
			$this->doProcessForm();
		}
	}
	
	private function showForm()
	{
		$contentFrame = $this->index('Seednia::'.self::MODULE_NAME, 'default_sbar_right');
		$content = $this->createView('mbox_form');
		$contentFrame->content = $content;
	
		$replyToSid = $this->request->query('replyToSid');
		$recipientXid = $this->request->query('recipientXid');
		
		$content->message = new stdClass();
		try {
			if ($replyToSid != NULL) {
				$replyingTo = $this->messageService->get($replyToSid);
				$content->message->subject = 'RE:'.$replyingTo->subject;
				$content->message->recipientDto = $replyingTo->recipientDto;
			}
			if ($recipientXid != NULL) {
				$recipientDto = $this->_profileHelper->getUserService()->getByXid($recipientXid);
				$content->message->recipientSid = $recipientDto->sid;
				$content->message->recipientDisplayName = $recipientDto->displayName;
			}
		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::Add Lis | Message: ' . $ex->getMessage());
		}
	}
	
	private function doProcessForm()
	{
		try {
			$record = $this->loadFormParams();
			
			$sid = $this->messageService->add($record);
			
			if ($sid != null) {
				; // TODO: handle error
			}

			$redirTo = controller_base_path($this->_requestContext, CONTROLLERPAGE).'/'.$this->request->controller();
			$this->request->redirect($redirTo);
		} catch (Exception $ex)
		{
			Kohana_Log::instance()->add(Kohana_Log::ERROR, 'Exception::doSend | Message: ' . $ex->getMessage());
		}
	}
	
	private function loadFormParams()
	{
		$record = array();
		
		$record['recipientSid'] = $this->request->post('recipientSid');
		$record['body'] = $this->request->post('body');
		$record['body'] = $this->request->post('body');

		return $record;
	}
	
} // End Controller_Home